package com.cos.shop.services.impl;

import com.cos.shop.dao.AddressDao;
import com.cos.shop.dao.impl.AddressDaoImpl;
import com.cos.shop.entity.Address;
import com.cos.shop.services.AddressServices;

import java.sql.SQLException;
import java.util.List;

/**
 * @author: COS
 * @time: 2022/2/24 14:34
 * @description:
 */
public class AddressServicesImpl implements AddressServices {
    private AddressDao ad = new AddressDaoImpl();

    @Override
    public boolean setDefaultAddress(int aid, int uid) {
        try {
            if (aid>0) {
                //1 先设置用户所有的地址都是非默认地址
                ad.setAddressNoneDefault(uid);
                //2 设置aid的地址为默认状态
                if (ad.setDefault(aid)>0) {
                    return true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean update(Address a) {
        try {
            if (ad.update(a)>0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean delete(int aid) {
        try {
            if (ad.delete(aid)>0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public void insertAddress(Address a) {
        try {
            ad.insertAddress(a);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Address> getAddressListByUid(int uid) {
        try {
            return ad.getAddressListByUid(uid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
